package com.mayn.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jd.open.api.sdk.DefaultJdClient;
import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.JdException;
import com.jd.open.api.sdk.request.order.OrderGetRequest;
import com.jd.open.api.sdk.response.order.OrderGetResponse;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.mayn.common.TradeObject;

/*
 * 京东订单查询
 */
public class JdTradeAction {
	private static final Logger LOG = LoggerFactory.getLogger(JdTradeAction.class);
	
	static String SERVER_URL = "https://api.jd.com/routerjson";
	/**
	 * 360buy.order.get
	 * 
	 * @param shopId
	 *            2,极途;11橙派
	 * @param tid
	 * @return
	 */
	public static TradeObject getTradeInfo(int shopId, String tid) {
		TradeObject jdpTbTradeObject = new TradeObject();
		String getShopSql = "select SHOP_ID,SHOP_NAME,SHOP_API_APP_KEY,SHOP_API_ACCESS_TOKEN,SHOP_API_APP_SECRECT from shop where SHOP_ID='" + shopId + "'";
		Record getShopRec = Db.findFirst(getShopSql);
		String seller_nick = "";
		if (getShopRec != null) {
			seller_nick = getShopRec.getStr("SHOP_NAME");
			String accessToken = getShopRec.getStr("SHOP_API_ACCESS_TOKEN");
			String appKey = getShopRec.getStr("SHOP_API_APP_KEY");
			String appSecret = getShopRec.getStr("SHOP_API_APP_SECRECT");
			
			JdClient client = new DefaultJdClient(SERVER_URL, accessToken, appKey, appSecret);
			OrderGetRequest request = new OrderGetRequest();
			request.setOrderId(tid);
			request.setOptionalFields(getOptionalFields());
			OrderGetResponse response = null;
			try {
				response = client.execute(request);
			} catch (JdException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (response != null && response.getCode().equals("0")) {
				String msg = "订单查询成功:" + shopId + "--" + seller_nick + "--" + tid;
				LOG.info(msg);
				jdpTbTradeObject.setRetCode(1);
				jdpTbTradeObject.setRetMsg(msg);
				jdpTbTradeObject.setTrade(response.getMsg());
				return jdpTbTradeObject;
			}else{
				String msg = "订单不存在:" + shopId + "--" + seller_nick + "--" + tid;
				LOG.error(msg);
				jdpTbTradeObject.setRetCode(2);
				jdpTbTradeObject.setRetMsg(msg);
				jdpTbTradeObject.setTrade("{}");
				return jdpTbTradeObject;
			}
		} else {
			String msg = "店铺不存在:" + shopId + "--" + seller_nick + "--" + tid;
			LOG.error(msg);
			jdpTbTradeObject.setRetCode(3);
			jdpTbTradeObject.setRetMsg(msg);
			jdpTbTradeObject.setTrade("{}");
			return jdpTbTradeObject;
		}
	}
	
	public static String getOptionalFields(){
		StringBuffer sb = new StringBuffer();
		sb.append("order_id").append(",");//订单id 
		sb.append("order_source").append(",");//订单来源 
		sb.append("customs").append(",");//保税区信息 
		sb.append("customs_model").append(",");//保税模型：直邮，保税集货，保税备货 
		sb.append("vender_id").append(",");//商家id 
		sb.append("pay_type").append(",");//支付方式（1货到付款, 2邮局汇款, 3自提, 4在线支付, 5公司转账, 6银行卡转账） 
		sb.append("order_total_price").append(",");//订单总金额 
		sb.append("order_seller_price").append(",");//订单货款金额（订单总金额-商家优惠金额） 
		sb.append("order_payment").append(",");//用户应付金额 
		sb.append("freight_price").append(",");//商品的运费 
		sb.append("seller_discount").append(",");//商家优惠金额 
		sb.append("order_state").append(",");//订单状态（英文） 
		sb.append("order_state_remark").append(",");//订单状态说明（中文） 
		sb.append("delivery_type").append(",");//送货（日期）类型（1-只工作日送货(双休日、假日不用送);2-只双休日、假日送货(工作日不用送);3-工作日、双休日与假日均可送货;其他值-返回“任意时间”） 
		sb.append("invoice_info").append(",");//发票信息 “invoice_info: 不需要开具发票”下无需开具发票；其它返回值请正常开具发票 
		sb.append("order_remark").append(",");//买家下单时订单备注 
		sb.append("order_start_time").append(",");//下单时间 
		sb.append("order_end_time").append(",");//结单时间 如返回信息为“0001-01-01 00:00:00”和“1970-01-01 00:00:00”，可认为此订单为未完成状态。 
		sb.append("modified").append(",");//订单更新时间 
		sb.append("consignee_info").append(",");//收货人基本信息 
		sb.append("item_info_list").append(",");// 
		sb.append("coupon_detail_list").append(",");//优惠详细信息 
		sb.append("vender_remark").append(",");//商家订单备注（不大于500字符） 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("balance_used").append(",");//余额支付金额 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("payment_confirm_time").append(",");//付款确认时间 如果没有付款时间 默认返回0001-01-01 00:00:00 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("waybill").append(",");//运单号(当厂家自送时运单号可为空，不同物流公司的运单号用|分隔，如果同一物流公司有多个运单号，则用英文逗号分隔) 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("logistics_id").append(",");//物流公司ID 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("vat_invoice_info").append(",");//增值税发票 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("parent_order_id").append(",");//父订单号 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("pin").append(",");//买家的账号信息 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("return_order").append(",");//售后订单标记 0:不是换货订单 1返修发货,直接赔偿,客服补件 2售后调货 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("order_type").append(",");//订单类型（22 SOP；23 LBP ；25 SOPL） 可选字段，需要在输入参数optional_fields中写入才能返回 
		sb.append("store_order").append("");//京仓订单.如果是京仓订单，结果返回文字“京仓订单”；否则返回结果为空值“” 
		return sb.toString();		
	}
}
